#include "ShaderSpecify.hpp"

namespace zzz{
//////////////////////////////////////////////////////////////////////////
//COLORSHADER
const char ColorShaderVert[]="\
varying vec4 color; \
void main(void)\
{\
  gl_Position=ftransform(); \
  color=gl_Color; \
}\
";
const char ColorShaderFrag[]="\
varying vec4 color; \
void main(void)\
{\
  gl_FragColor=color; \
}\
";
//////////////////////////////////////////////////////////////////////////
//DIFFUSESHADER
// NORMAL SHOULD NOT BE -
// BUT THE RESULT IS ALWAYS WRONG
// DON'T KNOW WHY
const char DiffuseShaderVert[]="\
varying vec4 color; \
varying vec3 N,v; \
void main(void)\
{\
  N=-gl_NormalMatrix*gl_Normal; \
  color=gl_Color; \
  gl_Position=gl_ModelViewProjectionMatrix * gl_Vertex; \
}\
";
const char DiffuseShaderFrag[]="\
varying vec4 color; \
varying vec3 N,v; \
void main(void)\
{\
  vec3 L=normalize(vec3(0,0,-100)); \
  gl_FragColor=color * max(dot(N,L),0.0); \
  gl_FragColor[3]=1; \
}\
";
//////////////////////////////////////////////////////////////////////////
//LIGHTSHADER
// NORMAL SHOULD NOT BE -
// BUT THE RESULT IS ALWAYS WRONG
// DON'T KNOW WHY
const char LightShaderVert[]="\
varying vec4 color; \
varying vec3 N,v; \
void main(void)\
{\
  v=vec3(gl_ModelViewMatrix*gl_Vertex); \
  N=-gl_NormalMatrix*gl_Normal; \
  color=gl_Color; \
  gl_Position=gl_ModelViewProjectionMatrix * gl_Vertex; \
}\
";
const char LightShaderFrag[]="\
varying vec4 color; \
varying vec3 N,v; \
void main(void)\
{\
  vec3 L=normalize(vec3(0,0,-100)-v); \
  vec3 E=normalize(-v); \
  vec3 R=-reflect(L,N); \
  gl_FragColor=color * max(dot(N,L),0.0) + color * pow(max(dot(R,E),0.0),50.0); \
  gl_FragColor[3]=1; \
}\
";
//////////////////////////////////////////////////////////////////////////
//TEXTURESHADER
const char TextureShaderVert[]="\
varying vec2 texc; \
void main(void)\
{\
  gl_Position=ftransform(); \
  texc=vec2(gl_MultiTexCoord0); \
}\
";
const char TextureShaderFrag[]="\
varying vec2 texc; \
uniform sampler2D tex; \
void main(void)\
{\
  gl_FragColor=texture2D(tex,texc); \
  gl_FragColor[3]=1; \
}\
";
//////////////////////////////////////////////////////////////////////////
//TEXTURELIGHTSHADER
const char TextureLightShaderVert[]="\
varying vec2 texc; \
varying vec4 color; \
varying vec3 N,v; \
void main(void)\
{\
  v=vec3(gl_ModelViewMatrix*gl_Vertex); \
  N=-gl_NormalMatrix*gl_Normal; \
  color=gl_Color; \
  gl_Position=gl_ModelViewProjectionMatrix * gl_Vertex; \
  texc=vec2(gl_MultiTexCoord0); \
}\
";
const char TextureLightShaderFrag[]="\
varying vec2 texc; \
uniform sampler2D tex; \
varying vec4 color; \
varying vec3 N,v; \
void main(void)\
{\
  vec3 L=normalize(vec3(0,0,-100)-v); \
  vec3 E=normalize(-v); \
  vec3 R=-reflect(L,N); \
  gl_FragColor=color * max(dot(N,L),0.0) + color * pow(max(dot(R,E),0.0),50.0); \
  gl_FragColor = gl_FragColor * texture2D(tex,texc); \
  gl_FragColor[3]=1; \
}\
";
//////////////////////////////////////////////////////////////////////////
//TEXCOORDSHADER
const char TexCoordShaderVert[]="\
varying vec2 texc; \
void main(void)\
{\
  gl_Position=ftransform(); \
  texc=vec2(gl_MultiTexCoord0); \
}\
";
const char TexCoordShaderFrag[]="\
varying vec2 texc; \
void main(void)\
{\
gl_FragColor=vec4(texc[0], texc[1], 0, 1); \
}\
";
//////////////////////////////////////////////////////////////////////////
//CUBEMAPSHADER
const char CubemapShaderVert[]="\
varying vec3 texc; \
void main(void)\
{\
  gl_Position=ftransform(); \
  texc=vec3(gl_MultiTexCoord0); \
}\
";
const char CubemapShaderFrag[]="\
varying vec3 texc; \
uniform samplerCube tex; \
void main(void)\
{\
  gl_FragColor=textureCube(tex,texc); \
}\
";

void MakeShaders()
{
  if (ZRM->IsExist("ColorShader")) return;

  printf("Prepare general shaders...\n");
  ZRM->Add(ShaderScript::LoadShaderMemory(ColorShaderVert,ColorShaderFrag,NULL),"ColorShader");
  ZRM->Add(ShaderScript::LoadShaderMemory(DiffuseShaderVert,DiffuseShaderFrag,NULL),"DiffuseShader");
  ZRM->Add(ShaderScript::LoadShaderMemory(LightShaderVert,LightShaderFrag,NULL),"LightShader");
  ZRM->Add(ShaderScript::LoadShaderMemory(TextureShaderVert,TextureShaderFrag,NULL),"TextureShader");
  ZRM->Add(ShaderScript::LoadShaderMemory(TextureLightShaderVert,TextureLightShaderFrag,NULL),"TextureLightShader");
  ZRM->Add(ShaderScript::LoadShaderMemory(TexCoordShaderVert,TexCoordShaderFrag,NULL),"TexCoordShader");
  ZRM->Add(ShaderScript::LoadShaderMemory(CubemapShaderVert,CubemapShaderFrag,NULL),"CubemapShader");
}
}